﻿//  --------------------------------
//  Copyright (c) AW Systems, Inc. All rights reserved.
//  This source code is made available under the terms of the Microsoft Public License (Ms-PL)
//  http://www.codeplex.com/SLAM/license
//  ---------------------------------

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using CustomSaveButton = AWS.SLAM.WebControls.SaveButton;

namespace AWS.SLAM.WebControls
{
	/// <summary>
	/// Loader control rendered on masterpage to load controls on all pages in SharePoint
	/// </summary>
	public class SaveButtonLoader : WebControl
	{
		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);
			//If OnSaveHandler is set, load scripts that will override default save button click events
			//with SLAM's SaveButton click event.
			if (SPContext.Current.FormContext != null && SPContext.Current.FormContext.OnSaveHandler != null)
			{
				ScriptLink.RegisterScriptAfterUI(this.Page, "Slam.SaveButton.js", false, true);
			}
		}

		protected override void OnLoad(EventArgs e)
		{
			//If OnSaveHandler is set, add SLAM SaveButton to page and a hidden "handle" control
			if (SPContext.Current.FormContext.FormMode == SPControlMode.Edit || SPContext.Current.FormContext.FormMode == SPControlMode.New)
			{	
				CustomSaveButton saveButton = new CustomSaveButton();
				saveButton.ID = "slamSaveButton";
				this.Page.Form.Controls.Add(saveButton);
				this.Page.Form.Controls.Add(new LiteralControl("<input type=\"hidden\" id=\"hdnSlamSaveButtonId\" value=\"" + saveButton.ClientID + "_ctl00_diidIOSaveItem\" />"));
			}
			base.OnLoad(e);
		}
	}
}
